package impl.repository;

import interfaces.repository.ProductApparelRepository;
import interfaces.repository.UsersRepository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;

import dao.AccessDatabase;
import domain.CartItem;
import domain.User;

public class StubUsersRepositoryImpl implements UsersRepository {

	@Override
	public boolean add(User user) {

		try {
			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			st.execute("INSERT INTO `users` (`login`,`senha`) VALUES ('"
					+ user.getLogin() + "','" + user.getPass() + "');");
			con.close();

			return true;
		} catch (Exception t) {

			return false;

		}
	}

	@Override
	public boolean delete(User user) {

		try {
			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			st.execute("DELETE FROM `users` WHERE `login`='" + user.getLogin()
					+ "'");
			con.close();

			return true;
		} catch (Exception t) {

			return false;

		}

	}

	@Override
	public User findBylogin(String login) {

		User user;

		try {

			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("SELECT * FROM user where login='"
					+ login + "';");

			user = new User();
			user.setLogin(rs.getString("login"));
			user.setPass("senha");

			con.close();

			return user;

		} catch (SQLException ex) {
			Logger.getLogger(ProductApparelRepository.class.getName()).log(
					Level.SEVERE, null, ex);

			return null;
		}

	}

	@Override
	public boolean checkUser(String login, String pass) {
		User user;

		try {

			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("SELECT * FROM user where login='"
					+ login + "';");

			user = new User();
			user.setLogin(rs.getString("login"));
			user.setPass(rs.getString("senha"));
			con.close();
			if (user.getLogin().equalsIgnoreCase(login)
					&& user.getPass().equalsIgnoreCase(pass)) {

				return true;
			} else {
				return false;
			}

		} catch (SQLException ex) {
			Logger.getLogger(ProductApparelRepository.class.getName()).log(
					Level.SEVERE, null, ex);
			return false;
		}

	}

}